//
//  ProductMainTableViewCell.m
//  片刻
//
//  Created by admin on 15/6/4.
//  Copyright (c) 2015年 iApple. All rights reserved.
//

#import "ProductMainTableViewCell.h"
#import "MacroHeader.h"
#import "UIImageView+WebCache.h"

@implementation ProductMainTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)dealloc
{
    [_mainImageView release];
    [_titleLabel release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 165, 260, 30)];
        _titleLabel = [[ESBaseLabel alloc] initWithFrame:CGRectMake(2 * kHorizen, 33 * kVertical, 52 * kHorizen, 6 * kVertical)];

        
        _mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 65 * kHorizen) / 2, 2 * kVertical, 65 * kHorizen, 30 * kVertical)];
        
        _buyButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _buyButton.frame = CGRectMake(56 *kHorizen, 34 *kVertical, 12 *kHorizen, 4 *kVertical);
        [_buyButton addTarget:self action:@selector(handleButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"立即购买" attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:10],NSForegroundColorAttributeName:[UIColor whiteColor]}];
        [_buyButton setAttributedTitle:attrString forState:(UIControlStateNormal)];
        [_buyButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        
//        _titleLabel.backgroundColor = [UIColor lightGrayColor];
//        _mainImageView.backgroundColor = [UIColor lightGrayColor];
        _buyButton.backgroundColor = [UIColor colorWithRed:119 / 255.0 green:182 / 255.0 blue:69 / 255.0 alpha:1.0];
        _buyButton.layer.cornerRadius = 10;
        
        [self.contentView addSubview:_mainImageView];
        [self.contentView addSubview:_titleLabel];
        [self.contentView addSubview:_buyButton];
        [attrString release];
        
    }
    self.selectionStyle =  UITableViewCellSelectionStyleNone;
    return self;
}

- (void)setModel:(ProductViewModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    _titleLabel.text = model.title;
    [_mainImageView sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    _titleLabel.textColor = [ESBaseLabel appearance].textColor?[ESBaseLabel appearance].textColor:[UIColor blackColor];
    _titleLabel.font = [ESBaseLabel appearance].font;
}

- (void)handleButtonAction:(UIButton *)sender {
    //可实现浏览器的跳转(应用程序间的跳转)
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_model.buyurl]];
}

@end
